/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.DccKpsp;
import me.zhengjie.modules.system.service.dto.DccQuestionDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.DccKpspRepository;
import me.zhengjie.modules.system.service.DccKpspService;
import me.zhengjie.modules.system.service.dto.DccKpspDto;
import me.zhengjie.modules.system.service.dto.DccKpspQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.DccKpspMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author cjl
* @date 2023-06-07
**/
@Service
@RequiredArgsConstructor
public class DccKpspServiceImpl implements DccKpspService {

    private final DccKpspRepository dccKpspRepository;
    private final DccKpspMapper dccKpspMapper;

    @Override
    public Map<String,Object> queryAll(DccKpspQueryCriteria criteria, Pageable pageable){
        Page<DccKpsp> page = dccKpspRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dccKpspMapper::toDto));
    }

    @Override
    public List<DccKpspDto> queryAll(DccKpspQueryCriteria criteria){
        return dccKpspMapper.toDto(dccKpspRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DccKpspDto findById(Integer id) {
        DccKpsp dccKpsp = dccKpspRepository.findById(id).orElseGet(DccKpsp::new);
        ValidationUtil.isNull(dccKpsp.getId(),"DccKpsp","id",id);
        return dccKpspMapper.toDto(dccKpsp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DccKpspDto create(DccKpsp resources) {
        return dccKpspMapper.toDto(dccKpspRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DccKpsp resources) {
        DccKpsp dccKpsp = dccKpspRepository.findById(resources.getId()).orElseGet(DccKpsp::new);
        ValidationUtil.isNull( dccKpsp.getId(),"DccKpsp","id",resources.getId());
        dccKpsp.copy(resources);
        dccKpspRepository.save(dccKpsp);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            dccKpspRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DccKpspDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DccKpspDto dccKpsp : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("状态", dccKpsp.getStatus());
            map.put("类别", dccKpsp.getType());
            map.put("视频链接", dccKpsp.getVedio());
            map.put("视频缩略图", dccKpsp.getVedioSuoluetu());
            map.put("简介", dccKpsp.getJainjie());
            map.put("操作者", dccKpsp.getOperator());
            map.put("排序", dccKpsp.getNum());
            map.put("创建时间", dccKpsp.getCreatedAt());
            map.put("修改时间", dccKpsp.getUpdatedAt());
            map.put("标题", dccKpsp.getTitle());
            map.put("内容", dccKpsp.getContent());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String, Object> queryAllFront(DccKpspQueryCriteria criteria, Pageable pageable) {
        Page<DccKpsp> page = dccKpspRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Map<String, Object> page1 = PageUtil.toPage(page.map(dccKpspMapper::toDto));
        List<DccKpspDto> content = (List<DccKpspDto>) page1.get("content");
        List<DccKpspDto> collect = content.stream().map(e -> {
            e.setContent(null);
            return e;
        }).collect(Collectors.toList());
        page1.put("content",collect);

        return page1;
    }
}